import "package:android_studio_f/generated/json/base/credential_list_item_entity.dart";
import "package:flutter/material.dart";

class EditCredential extends StatefulWidget {
  const EditCredential({super.key, this.detail});

  final CredentialListItemEntity? detail;

  @override
  State<StatefulWidget> createState() => _EditCredential();
}

class _EditCredential extends State<EditCredential> {
  bool editing = false;

  void showVisitedDevice() {}

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title:
              Text(widget.detail?.id != null ? "${widget.detail!.id}" : "新增分享"),
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          actions: [
            !editing
                ? IconButton(
                    onPressed: showVisitedDevice,
                    icon: Icon(Icons.device_unknown))
                : SizedBox(),
            IconButton(
                onPressed: () {
                  setState(() {
                    editing = !editing;
                  });
                },
                icon: Icon(editing ? Icons.save : Icons.edit)),
          ],
        ),
        body: CredentialForm(
          detail: widget.detail,
          editing: editing,
        ));
  }
}

class DetailInfo extends StatelessWidget {
  // required this.editing
  const DetailInfo({
    super.key,
    required this.detail,
  });

  // final bool editing;
  final CredentialListItemEntity detail;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ListTile(
          title: Text(detail.corporateName),
          subtitle: Text(detail.createAt),
        )
      ],
    );
  }
}

class CredentialForm extends StatefulWidget {
  final bool editing;
  final CredentialListItemEntity? detail;

  const CredentialForm({super.key, required this.editing, this.detail});

  @override
  State<StatefulWidget> createState() => _CredentialForm();
}

class _CredentialForm extends State<CredentialForm> {
  final TextEditingController _nameCtl = TextEditingController();
  final _formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _nameCtl.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.detail != null && !widget.editing) {
      return DetailInfo(detail: widget.detail!);
    }
    if (widget.detail != null) {
      _nameCtl.text = widget.detail!.corporateName;
    }
    return Form(
        key: _formKey,
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16),
              child: TextFormField(
                keyboardType: TextInputType.name,
                controller: _nameCtl,
                autofocus: true,
                validator: (value) {
                  return null;
                },
                decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: "分享目标名",
                    hintText: "请输入公司名称"),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
              child: TextFormField(
                keyboardType: TextInputType.number,
                validator: (value) {
                  return null;
                },
                decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: '有效时长',
                    hintText: "请输入链接有效时长"),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
              child: Row(children: [
                Expanded(
                    flex: 1,
                    child: ElevatedButton(
                        onPressed: () {
                          bool pass = _formKey.currentState!.validate();
                          print("通过检验:${pass}");
                        },
                        child: Text("提交")))
              ]),
            )
          ],
        ));
  }
}
